//
//  WorkflowMenuListViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/1.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#define TESTLAYOUT_NUMBER 1

#import "WorkflowMenuListViewController.h"
#import "TangramView.h"
#import "TangramLayoutProtocol.h"
#import "TangramSingleAndDoubleLayout.h"
#import "TangramFlowLayout.h"
#import "TangramFixLayout.h"
#import "TangramStickyLayout.h"
#import "TangramFixTopLayout.h"
#import "TangramWaterFlowLayout.h"
#import "TangramDragableLayout.h"
#import "WorkflowListView.h"
#import "WorkflowMenuView.h"
#import "WorkflowListViewController.h"
#import "WorkflowApplyViewController.h"
#import "WorkflowToDoListViewController.h"

@interface WorkflowItemModel : NSObject<TangramItemModelProtocol>

@property   (nonatomic, assign) CGRect          itemModelFrame;
@property   (nonatomic, assign) BOOL            isBlock;
@property   (nonatomic, assign) NSUInteger      indexInLayout;

@end

@interface WorkflowItem : UIView

@property   (nonatomic, strong) WorkflowItemModel   *itemModel;

@end

@interface WorkflowLayout : TangramFlowLayout

@property   (nonatomic, assign) NSUInteger      index;

@end

@implementation WorkflowItemModel

- (void)setItemFrame:(CGRect)itemFrame
{
    _itemModelFrame = itemFrame;
}
- (CGRect)itemFrame
{
    return CGRectMake(_itemModelFrame.origin.x,_itemModelFrame.origin.y, _itemModelFrame.size.width, _itemModelFrame.size.height);
}

- (NSString *)display
{
    return @"inline";
}

- (TangramItemType *)itemType
{
    return @"demo";
}

- (NSString *)reuseIdentifier
{
    return @"demo_model_reuse_identifier";
}
- (CGFloat)marginTop
{
    return 5.f;
}
- (CGFloat)marginRight
{
    return 5.f;
}
- (CGFloat)marginBottom
{
    return 5.f;
}
- (CGFloat)marginLeft
{
    return 5.f;
}
@end

@implementation WorkflowItem

- (NSObject<TangramItemModelProtocol> *)model
{
    return _itemModel;
}
@end

@implementation WorkflowLayout

- (TangramLayoutType *)layoutType
{
    return [NSString stringWithFormat:@"xxxxx_%lu", self.index];
}

@end

@interface WorkflowMenuListViewController ()<TangramViewDatasource,TangramViewDelegate,WorkflowListViewdelegate,WorkflowMenuViewdelegate>
{
    NSMutableArray *_datas;
    RSWDataOperation *_manager;
}

@property (nonatomic, assign) NSUInteger totalIndex;

@end

@implementation WorkflowMenuListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    [self checkSession];
    
    [self setNavigateItemTitle:@"工作流"];
    
    [self initVariable];
    
    [_manager getWorkflowListCompletion:^(NSInteger nResult, NSArray *output, NSError *error){
        [self requestComplete:nil result:nResult output:output error:error];
    }];
}

- (void)initVariable
{
    _datas = [NSMutableArray new];
    _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];
}

- (void)initUI
{
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    
    self.totalIndex = 0;
    TangramView *tangramView = [[TangramView alloc] initWithFrame:
                                CGRectMake(0, top, self.view.width, self.view.height - top - ([UIDevice is58InchScreen]?83:49))];
    
    tangramView.dataSource  = self;
    tangramView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tangramView];
    tangramView.fixExtraOffset = 64.f;
    [tangramView setDelegate:self];
    [tangramView reloadData];
}

-(void)requestComplete:(XMSDataOperationManager *)dom result:(NSInteger)nResult output:(id)output error:(NSError *)error
{
    [self filterRequestResult:nResult defaultError:error noFilter:^(NSInteger requestResult)
     {
         switch(requestResult)
         {
             case YES:
             {
                 _datas = output;
                 
                 [self initUI];
                 
                 break;
             }
             default:
             {
                 break;
             }
         }
     }];
}

#pragma mark - TangramViewDatasource
- (UIView *)itemInTangramView:(TangramView *)view withModel:(NSObject<TangramItemModelProtocol> *)model forLayout:(UIView<TangramLayoutProtocol> *)layout atIndex:(NSUInteger)index
{
    //首先查找是否有可以复用的Item,是否可以复用是根据它的reuseIdentifier决定的
    //layout不复用，复用的是item
    WorkflowItem *item = (WorkflowItem *)[view dequeueReusableItemWithIdentifier:model.reuseIdentifier];
    if (nil == item) {
        item = [[WorkflowItem alloc] initWithFrame:CGRectMake(0.f, 0.f, 0.f, 0.f) reuseIdentifier:model.reuseIdentifier];
    }
    
    if(index == 0)
    {
        WorkflowListView *listView = [[WorkflowListView alloc] initWithFrame:item.frame userInteractionEnabled:YES];
        listView.headImgUrl = @"";
        listView.title = @"全部事项";
        listView.backgroundColor = [self firstColor];
        listView.delegate = self;
        listView.tag = index;
        
        listView.szTag = @"AllItems";
        
        return listView;
    }
    else if(index == 1)
    {
        WorkflowListView *listView = [[WorkflowListView alloc] initWithFrame:item.frame userInteractionEnabled:YES];
        listView.headImgUrl = @"";
        listView.title = @"待办事项";
        listView.backgroundColor = [self firstColor];
        listView.delegate = self;
        listView.tag = index;
        
        listView.szTag = @"ToDoItems";
        
        return listView;
    }
    else
    {
        WorkflowInfo *info = _datas[index - 2];
        
        WorkflowMenuView *listView = [[WorkflowMenuView alloc] initWithFrame:item.frame userInteractionEnabled:YES];
        listView.headImgUrl = info.icon;
        listView.title = info.name;
        
        NSInteger mode = (index - 2)%6;
        listView.backgroundColor = [self getColor:mode];
        
        listView.delegate = self;
        listView.tag = index;
        
        listView.szTag = info.processCode;
        
        return listView;
    }
}

//Layout不做复用，复用的是Item
- (UIView<TangramLayoutProtocol> *)layoutInTangramView:(TangramView *)view atIndex:(NSUInteger)index
{
    //普通流式布局
    WorkflowLayout *layout = [[WorkflowLayout alloc] init];
    layout.margin = @[@0,@0,@0,@0];
    layout.numberOfColumns = 2;
    layout.index = index;
    layout.backgroundColor = [UIColor whiteColor];
    return layout;
}

- (NSObject<TangramItemModelProtocol> *)itemModelInTangramView:(TangramView *)view forLayout:(UIView<TangramLayoutProtocol> *)layout atIndex:(NSUInteger)index
{
    WorkflowItemModel *model = [[WorkflowItemModel alloc] init];
    model.indexInLayout = index;
    [model setItemFrame:CGRectMake(model.itemFrame.origin.x,model.itemFrame.origin.y, model.itemFrame.size.width, 150)];
    if (index == 0 || index == 1) {
        [model setItemFrame:CGRectMake(model.itemFrame.origin.x,model.itemFrame.origin.y, model.itemFrame.size.width, 100)];
    }
    return model;
}

- (NSUInteger)numberOfLayoutsInTangramView:(TangramView *)view
{
    return TESTLAYOUT_NUMBER;
}

- (NSUInteger)numberOfItemsInTangramView:(TangramView *)view forLayout:(UIView<TangramLayoutProtocol> *)layout
{
    return 2 + _datas.count;
}

#pragma mark -- Private

- (UIColor *)randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 );
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

-(UIColor*)getColor:(NSInteger)index
{
    UIColor *color = [self firstColor];
    switch (index) {
        case 0:
            color = [self firstColor];
            break;
        case 1:
            color = [self secondColor];
            break;
        case 2:
            color = [self thirdColor];
            break;
        case 3:
            color = [self fourthColor];
            break;
        case 4:
            color = [self fifthColor];
            break;
        case 5:
            color = [self sixthColor];
            break;
        default:
            break;
    }
    
    return color;
}

-(UIColor*)firstColor
{
    return [UIColor colorWithR:252.0 G:81.0 B:98.0f A:1.0];
}

-(UIColor*)secondColor
{
    return [UIColor colorWithR:61.0 G:198.0 B:159.0f A:1.0];
}

-(UIColor*)thirdColor
{
    return [UIColor colorWithR:248.0 G:169.0 B:46.0f A:1.0];
}

-(UIColor*)fourthColor
{
    return [UIColor colorWithR:141.0 G:104.0 B:234.0f A:1.0];
}

-(UIColor*)fifthColor
{
    return [UIColor colorWithR:71.0 G:120.0 B:240.0f A:1.0];
}

-(UIColor*)sixthColor
{
    return [UIColor colorWithR:130.0 G:201.0 B:61.0f A:1.0];
}

#pragma mark -- WorkflowListViewdelegate
-(void)clickWorkflowListView:(WorkflowListView*)workflowListView
{
    XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
    
    if (workflowListView.tag == 0)
    {
        [XUINavigationController pushViewControllerClass:[WorkflowListViewController class] withSwitchParam:p];
    }
    else
    {
        [XUINavigationController pushViewControllerClass:[WorkflowToDoListViewController class] withSwitchParam:p];
    }
}

#pragma mark -- WorkflowMenuViewdelegate
-(void)clickWorkflowMenuViewAllBtn:(WorkflowMenuView*)workflowListView
{
    XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
    p.param[WorkflowListViewControllerCodeParametersParamKey] =  workflowListView.szTag;
    
    [XUINavigationController pushViewControllerClass:[WorkflowListViewController class] withSwitchParam:p];
}
-(void)clickWorkflowMenuViewToDoBtn:(WorkflowMenuView*)workflowListView
{
    XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
    p.param[WorkflowToDoListViewControllerCodeParametersParamKey] =  workflowListView.szTag;
    
    [XUINavigationController pushViewControllerClass:[WorkflowToDoListViewController class] withSwitchParam:p];
}

-(void)clickWorkflowMenuViewApplyBtn:(WorkflowMenuView*)workflowListView
{
    WorkflowInfo *info = _datas[workflowListView.tag - 2];
    if(info != nil)
    {
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        p.param[WorkflowApplyViewControllerWorkflowInfoParametersParamKey] =  info;
        
        [XUINavigationController pushViewControllerClass:[WorkflowApplyViewController class] withSwitchParam:p];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
